package cn.soqi.gui;
/**
 * 文件操作
 *
 * @author brozer
 * @email 798121446@qq.com
 * @create 2017-05-08 15:05
 */

import cn.soqi.img.ZipDeCompress;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

@SuppressWarnings("serial")
public class FileChooser extends JFrame implements ActionListener {
    JButton open = null;
    JButton destination=null;
    JTextField openPath = null;
    JTextField destinationPath = null;
    JButton confir=null;
    public static void main(String[] args) {
        new FileChooser();
    }

    public FileChooser() {

        JPanel contentPane=new JPanel();
        contentPane.setBorder(new EmptyBorder(5,5,5,5));
        this.setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(3,1,5,5));
        JPanel pane1=new JPanel();
        contentPane.add(pane1);
        JPanel pane2=new JPanel();
        contentPane.add(pane2);
        JPanel pane3=new JPanel();
        contentPane.add(pane3);
        open = new JButton("zip文件地址");
        openPath=new JTextField();
        pane1.add(openPath);
        pane1.add(open);
        destination=new JButton("转换目标地址");
        destinationPath=new JTextField();
        pane2.add(destinationPath);
        pane2.add(destination);
        confir=new JButton("开始转换");
        pane3.add(confir);
        openPath. setColumns(10);
        destinationPath.setColumns(10);
        this.setBounds(400, 200, 600, 400);
        this.setLayout(new GridLayout(3,1,1,1));
        this.setVisible(true);
        this.setTitle("RgbToCmyk  www.soqi.cn");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        open.addActionListener(this);
        destination.addActionListener(this);

        confir.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("开始转换...");
                        try {
                            ZipDeCompress.deCompress(openPath.getText().trim(),destinationPath.getText().trim());
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }
                    }
                });


    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        String source = e.getActionCommand();
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), "选择");
        jfc.setFileFilter(new ZipFileFilter());
        File file = jfc.getSelectedFile();
        if(file!=null) {
            if (file.isDirectory()) {
                if (source.equals("zip文件地址")) {
                   // openPath.setText(file.getAbsolutePath());
                } else {
                    destinationPath.setText(file.getAbsolutePath());
                }
            } else if (file.isFile()) {
                if (source.equals("zip文件地址")) {
                    openPath.setText(file.getAbsolutePath());
                } else {
                    //openPath.setText(file.getAbsolutePath());
                }
            }
            System.out.println(jfc.getSelectedFile().getName());
        }
    }






}